#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create type_time: 2020-2-24
Info: 定期向企业微信推送消息
"""
import requests, json
import datetime
import time
import base64,hashlib
from jili.core.pymsg import mohe

from functools import partial
#sendfault=partial(msg.send,msgtype,subject)
class chat_qywx:
    def __init__(self,msgtype):
        self.msg = mohe(None, 'tcp://121.199.56.237:9115', 1)
        self.send=partial(self.msg.send,msgtype,"ibot_qywx")
    def send_msg(self,msg,at_name_list=[],at_phone_list=[]):
        """艾特全部，并发送指定信息"""
        data = {"msgtype": "text", "text": {"content": msg, "mentioned_list":at_name_list,"mentioned_mobile_list":at_phone_list}}
        self.send(data)
    def send_markdown(self,markdown):
        data = {"msgtype": "markdown", "markdown": {"content": markdown}}
        self.send(data)
    def send_image(self,image_url):
        with open(image_url,"rb") as f:
            s=f.read()
            base64_data=base64.b64encode(s)
            data_base64=base64_data.decode()
            md=hashlib.md5()
            md.update(s)
            data_md5=md.hexdigest()
            data = {"msgtype": "image", "image": {"base64": data_base64,
            "md5": data_md5}}
            self.send(data)
    def send_news(self,title,description,url,picurl):
        data = {"msgtype": "news", "news": {"articles": [{
               "title" : title,
               "description" : description,
               "url" : url,
               "picurl" : picurl
           }]}}
        self.send(data)
    def send_file(self,file_url):
        with open(file_url,"rb") as f:
            data={"file":f.read()}
            self.send(data)

if __name__ == '__main__':
    chat=chat_qywx("strong_stock")
    chat.send_msg("我只测试下,lallala",at_phone_list=["13902101481"])
    chat.send_image(r"G:\pos\future3\c1605_k1m(3, 7)2ctype0overtype0fee_size2openslippage0.5ktype0001delta3.jpg")
    chat.send_file(r"G:\pos\future1\TA005_k1m282ctype0overtype1fee_size2open.2.jpg")
